package com.seio.edc.controller.system;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.nova.edc.model.system.CustomerRegion;
import com.nova.edc.service.system.CustomerRegionService;
import com.nova.edc.service.system.RegionService;
import com.nova.frame.dao.pager.Page;
import com.nova.frame.dao.pager.PageList;
import com.nova.frame.utils.DateUtils;
import com.seio.edc.controller.core.BaseContoller;

@Controller
public class CompanyRegionContronller extends BaseContoller {

	@Autowired
	private com.nova.edc.service.system.CustomerInfoService novaCustomerInfoService;

	@Autowired
	private CustomerRegionService novaCustomerRegionService;

	@Autowired
	private RegionService novaRegionService;

	private final int PAGE_SIZE = 10;

	/**
	 * 公司位置页面初始化
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "companyRegionInit.shtml")
	public ModelAndView companyRegionInit(HttpServletRequest request,
			HttpSession session, Integer pageIndex, ModelMap modelMap,
			@ModelAttribute("customerRegion") CustomerRegion customerRegion) {
		// 获取数据为当前登录用户添加的数据
		String loginName = (String) session.getAttribute("loginname");
		Page page = new Page();
		page.setCurrentPage(pageIndex == null || pageIndex == 0 ? 1 : pageIndex);
		page.setPageSize(PAGE_SIZE);
		modelMap.put("pageIndex", page.getCurrentPage());
		// 公司名称
		modelMap.put("companyNameList",
				novaCustomerInfoService.findCustomerInfoByLogin(loginName));
		// 省、市、自治区
		modelMap.put("chinamapList", novaRegionService.findAll());
		PageList<CustomerRegion> pageList = novaCustomerRegionService
				.findPageList(customerRegion, page, "AddDate", "desc");
		modelMap.put("dataCount", page.getRowCount());
		modelMap.put("pageCount", page.getTotalPage());
		modelMap.put("list", pageList.getDatas());

		return new ModelAndView("system/companyRegionList", modelMap);
	}

	/**
	 * 公司位置新增
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "companyRegionAdd.shtml")
	public ModelAndView companyRegionAdd(HttpServletRequest request,
			HttpSession session, ModelMap modelMap, Integer id) {
		// 获取数据为当前登录用户添加的数据
		String loginName = (String) session.getAttribute("loginname");
		// 公司名称
		modelMap.put("companyNameList",
				novaCustomerInfoService.findCustomerInfoByLogin(loginName));
		// 省、市、自治区
		modelMap.put("chinamapList", novaRegionService.findAll());
		// 更新数据时获取数据
		if (id != null && id != 0) {
			modelMap.put("customerRegion",
					novaCustomerRegionService.getById(id));
		}
		return new ModelAndView("system/companyRegionEdit", modelMap);
	}

	/**
	 * 公司位置保存
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "companyRegionUpdate.shtml")
	public ModelAndView companyRegionUpdate(HttpServletRequest request,
			HttpSession session, ModelMap modelMap,
			CustomerRegion customerRegion) {

		// 获取数据为当前登录用户添加的数据
		String loginName = (String) session.getAttribute("loginname");
		if (customerRegion.getId() != null && customerRegion.getId() != 0) {
			customerRegion.setModifyby(loginName);
			customerRegion.setModifydate(DateUtils.getCurrentDate());
			customerRegion.setModifystamp(DateUtils.getCurrentDate());
			novaCustomerRegionService.update(customerRegion);
		} else {
			customerRegion.setAddby(loginName);
			customerRegion.setAdddate(DateUtils.getCurrentDate());
			novaCustomerRegionService.insert(customerRegion);
		}

		return companyRegionInit(request, session, 1, modelMap,
				new CustomerRegion());
	}

	/**
	 * 公司位置删除
	 * 
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping(value = "companyRegionDelete.shtml")
	public ModelAndView companyRegionDelete(HttpServletRequest request,
			HttpSession session, ModelMap modelMap, Integer id) {
		if (id != null && id != 0) {
			novaCustomerRegionService.deleteById(id);
		}
		return companyRegionInit(request, session, 1, modelMap,
				new CustomerRegion());
	}
}
